Passed
Push — develop ( db4836...f63737 )
by Elvis Henrique
02:35
created

utils.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
nop 2
1
/**
2
 * The external dependencies.
3
 */
4
const fs = require('fs');
5
const path = require('path');
6
const crypto = require('crypto');
7
8
/**
9
 * User config cache.
10
 */
11
let userConfig = null;
12
13
/**
14
 * API.
15
 */
16
module.exports.themeRootPath = (basePath = '', destPath = '') =>
17
  path.resolve(path.dirname(__dirname), '../../', basePath, destPath);
18
19
module.exports.srcPath = (basePath = '', destPath = '') =>
20
  path.resolve(path.dirname(__dirname), '../', basePath, destPath);
21
22
module.exports.distPath = (basePath = '', destPath = '') =>
23
  path.resolve(path.dirname(__dirname), '../../dist', basePath, destPath);
24
25
module.exports.srcScriptsPath = destPath => exports.srcPath('scripts', destPath);
26
27
module.exports.srcStylesPath = destPath => exports.srcPath('styles', destPath);
28
29
module.exports.srcImagesPath = destPath => exports.srcPath('images', destPath);
30
31
module.exports.srcFontsPath = destPath => exports.srcPath('fonts', destPath);
32
33
module.exports.srcVendorPath = destPath => exports.srcPath('vendor', destPath);
34
35
module.exports.distScriptsPath = destPath => exports.distPath('scripts', destPath);
36
37
module.exports.distStylesPath = destPath => exports.distPath('styles', destPath);
38
39
module.exports.distImagesPath = destPath => exports.distPath('images', destPath);
40
41
module.exports.distFontsPath = destPath => exports.distPath('fonts', destPath);
42
43
module.exports.tests = {
44
  scripts: /\.(js|jsx)$/,
45
  styles: /\.(css|scss)$/,
46
  images: /images[\\/].*\.(ico|jpg|jpeg|png|svg|gif)$/,
47
  fonts: /fonts[\\/].*\.(eot|svg|ttf|woff|woff2)$/,
48
};
49
50
module.exports.detectEnv = () => {
51
  const env = process.env.NODE_ENV || 'development';
52
  const isDev = env === 'development';
53
  const isProduction = env === 'production';
54
55
  return {
56
    env,
57
    isDev,
58
    isProduction,
59
  };
60
};
61
62
module.exports.getUserConfig = () => {
63
  const userConfigPath = path.join(process.cwd(), 'config.json');
64
65
  if (userConfig !== null) {
66
    return userConfig;
67
  }
68
69
  if (!fs.existsSync(userConfigPath)) {
70
    console.log('\x1B[31mCould not find your config.json file. Please make a copy of config.json.dist and adjust as needed.\x1B[0m');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
71
    process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
72
  }
73
74
  try {
75
    userConfig = JSON.parse(fs.readFileSync(userConfigPath));
76
  } catch (e) {
77
    console.log('\x1B[31mCould not parse your config.json file. Please make sure it is a valid JSON file.\x1B[0m');
78
    process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
79
  }
80
81
  return userConfig;
82
};
83
84
module.exports.filehash = (file) => {
85
  const hash = crypto.createHash('sha1');
86
  hash.update(fs.readFileSync(file));
87
  return hash.digest('hex');
88
};
89